home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
-
- class StateMachine:
- '''
- '''
- STATE_IDLE = 'IDLE'
- STATE_CONNECTING = 'CONNECTING'
- options = None
- __positionCommand = 'position startpos'
- __haveMoves = False
- buffer = ''
- __readyToConfigure = False
- __options = None
- __ready = False
- __inCallback = False
- __queuedCommands = None
-
- def __init__(self):
- '''
- '''
- self.options = { }
- self._StateMachine__queuedCommands = []
-
-
- def logText(self, text, style):
- '''
- '''
- pass
-
-
- def onOutgoingData(self, data):
- '''
- '''
- pass
-
-
- def onMove(self, move):
- """Called when the AI makes a move.
-
- 'move' is the move the AI has decided to make (string).
- """
- print 'UCI move: ' + move
-
-
- def registerIncomingData(self, data):
- '''
- '''
- self._StateMachine__inCallback = True
- self.buffer += data
- while True:
- index = self.buffer.find('\n')
- if index < 0:
- break
-
- line = self.buffer[:index]
- self.buffer = self.buffer[index + 1:]
- self.parseLine(line)
- self._StateMachine__inCallback = False
- if self._StateMachine__options is not None and self._StateMachine__readyToConfigure:
- options = self._StateMachine__options
- self._StateMachine__options = None
- self.configure(options)
-
- if len(self._StateMachine__queuedCommands) > 0 and self._StateMachine__ready:
- commands = self._StateMachine__queuedCommands
- self._StateMachine__queuedCommands = []
- for c in commands:
- self._StateMachine__sendCommand(c)
-
-
-
-
- def __sendCommand(self, command):
- '''
- '''
- if self._StateMachine__ready and not (self._StateMachine__inCallback):
- self.onOutgoingData(command + '\n')
- else:
- self._StateMachine__queuedCommands.append(command)
-
-
- def start(self):
- '''
- '''
- self.onOutgoingData('uci\n')
-
-
- def startGame(self):
- '''
- '''
- self._StateMachine__sendCommand('ucinewgame')
- self._StateMachine__sendCommand(self._StateMachine__positionCommand)
-
-
- def configure(self, options = []):
- '''
- '''
- if not self._StateMachine__readyToConfigure:
- self._StateMachine__options = options
- return None
- for option in options:
- if option.value == '':
- continue
-
- self.onOutgoingData('setoption ' + option.name + ' value ' + option.value + '\n')
-
- self.onOutgoingData('isready\n')
-
-
- def requestMove(self, whiteTime, blackTime, ownTime):
- '''
- '''
- if whiteTime == 0:
- whiteTime = 30000
-
- if blackTime == 0:
- blackTime = 30000
-
- self._StateMachine__sendCommand('go wtime %d btime %d' % (whiteTime, blackTime))
-
-
- def undoMove(self):
- '''
- '''
- self._StateMachine__sendCommand('stop')
- (self._StateMachine__positionCommand, _) = self._StateMachine__positionCommand.rsplit(' ', 1)
- if self._StateMachine__positionCommand.endswith(' moves'):
- self._StateMachine__haveMoves = False
- self._StateMachine__positionCommand = 'position startpos'
-
- self._StateMachine__sendCommand(self._StateMachine__positionCommand)
-
-
- def reportMove(self, move, isSelf):
- '''
- '''
- if not self._StateMachine__haveMoves:
- self._StateMachine__positionCommand += ' moves'
-
- self._StateMachine__haveMoves = True
- self._StateMachine__positionCommand += ' ' + move
- self._StateMachine__sendCommand(self._StateMachine__positionCommand)
-
-
- def parseLine(self, line):
- '''
- '''
- words = line.split()
- while True:
- if len(words) == 0:
- self.logText(line + '\n', 'input')
- return None
- style = self.parseCommand(words[0], words[1:])
- if style is not None:
- self.logText(line + '\n', style)
- return None
- print 'WARNING: Unknown command: ' + repr(words[0])
- words = words[1:]
- continue
- len(words) == 0
-
-
- def parseCommand(self, command, args):
- '''
- '''
- if command == 'id':
- return 'info'
- if command == 'uciok':
- self._StateMachine__readyToConfigure = True
- return 'info'
- if command == 'readyok':
- if len(args) != 0:
- print 'WARNING: Arguments on readyok: ' + str(args)
-
- self._StateMachine__ready = True
- return 'info'
- if command == 'bestmove':
- if len(args) == 0:
- print 'WARNING: No move with bestmove'
- return 'error'
- move = args[0]
- self.onMove(move)
- return 'move'
- command == 'bestmove'
- if command == 'info':
- return 'info'
- if command == 'option':
- return 'info'
-
-
-